package com.cyrus.vueboot.config.oauth2;

import com.cyrus.vueboot.service.impl.UserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Author: Cyrus
 * @CreateDate: 2020/11/14
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    public  void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/toLogin","/swagger-ui.html").permitAll()//放行所有
                .antMatchers("/").hasRole("employee")
                .antMatchers("/page/Admin","/page/Authority","/page/OpsLog","/page/LoginLog").hasRole("super")
                .antMatchers("/page/department","/page/departmentUser").hasAnyRole("super","dptadmin","employee");
                           //跳转页面                            //登陆请求
        http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login");

        http.csrf().disable();//关闭csrf攻击防御
        //注销功能      注销跳转页面
        http.logout().logoutSuccessUrl("/toLogin");
        //默认不能进行frame跳转，需要关闭！
        http.headers().frameOptions().disable();



    }

    @Override
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
